<!--
 * @Author: 朱安武 549410045@qq.com
 * @Date: 2023-01-11 11:54:33
 * @LastEditors: 朱安武 549410045@qq.com
 * @LastEditTime: 2023-02-02 09:28:37
 * @FilePath: \mobile\src\pages\LoginPage.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
    <div>
        <van-nav-bar title="登录页" left-text="返回" left-arrow @click-left="onClickLeft" />
        <van-form @submit="onSubmit">
            <van-cell-group inset>
                <van-field v-model="username" name="username" label="手机号" placeholder="手机号"
                    :rules="[{ required: true, message: '请填写用户名' }]" />
                <van-field v-model="password" type="password" name="密码" label="密码" placeholder="密码"
                    :rules="[{ required: true, message: '请填写密码' }]" />
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    提交
                </van-button>
            </div>
        </van-form>
        <div class="register">去注册
            <van-button type="primary" size="large" color="#fffff" plain to="register" class="btn">快速注册<van-icon
                    name="arrow" /></van-button>
        </div>
    </div>
</template>

<script setup lang="ts">
import { showNotify } from 'vant'
import { ref } from 'vue'
import { router } from '../router'
import { loginUser } from '../services';

//设置类型
type userType = {
    username: string,
    password: string
}

const username = ref('');
const password = ref('');
const onSubmit = async (values: userType) => {
    values.username === localStorage.getItem('username')
    //登录

    //解构数据
    const { data } = await loginUser(username.value, password.value)
    //判断数据
    if (data.code === '200') {
        showNotify({ message: '登录成功' });
        //存储数据
        localStorage.setItem('token', data.data.token)
        localStorage.setItem('userid', data.data.userid)
        //成功跳转
        router.push('/home')
    } else {
        showNotify({ message: `${data.message}` });
    }

}

//返回上一级
const onClickLeft = () => history.back();
</script>

<style lang="scss" scoped>
.register {
    padding: 10px;
    font-size: 14px;
    color: gray;

    .btn {
        margin-top: 10px;
        border-radius: 15px;
    }
}
</style>